#ifndef RGB32WIDGET_H
#define RGB32WIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLShaderProgram>
#include <QOpenGLFunctions>

class RGB32Widget : public QOpenGLWidget, protected QOpenGLFunctions
{
public:
    explicit RGB32Widget(QWidget *parent = nullptr) : QOpenGLWidget(parent) {}

    void updateFrame(const uint8_t* data, int width, int height) {
        m_width = width;
        m_height = height;
        m_data = data;
        update();
    }

protected:
    void initializeGL() override {
        initializeOpenGLFunctions();

        // 编译着色器
        m_program.addShaderFromSourceCode(QOpenGLShader::Vertex,
            "#version 330 core\n"
            "layout(location = 0) in vec4 vertexIn;\n"
            "layout(location = 1) in vec2 textureIn;\n"
            "out vec2 textureCoord;\n"
            "uniform float aspect;\n"
            "void main() {\n"
            "    vec4 pos = vertexIn;\n"
            "    pos.x *= aspect;\n"  // 宽高比修正
            "    gl_Position = pos;\n"
            "    textureCoord = textureIn;\n"
            "}");

        m_program.addShaderFromSourceCode(QOpenGLShader::Fragment,
            "#version 330 core\n"
            "in vec2 textureCoord;\n"
            "out vec4 fragColor;\n"
            "uniform sampler2D tex;\n"
            "void main() {\n"
            "    fragColor = texture(tex, textureCoord);\n"
            "}");
        m_program.link();

        // 初始化纹理
        glGenTextures(1, &m_tex);
    }

    void resizeGL(int w, int h) override {
        glViewport(0, 0, w, h);
        m_aspect = (h > 0) ? (float)w/h : 1.0f;
    }

    void paintGL() override {
        if (!m_data || m_width <= 0 || m_height <= 0) return;

        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        m_program.bind();
        m_program.setUniformValue("aspect", m_aspect);

        // 设置纹理参数
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, m_tex);

        // 更新纹理数据（根据具体格式选择）
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,   // 内部格式
                    m_width, m_height, 0,
                    GL_BGRA,          // 数据格式（根据实际数据调整）
                    GL_UNSIGNED_BYTE, m_data);

        // 设置纹理参数
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        m_program.setUniformValue("tex", 0);

        // 顶点和纹理坐标
        static const GLfloat vertices[] = {
            -1.0f, -1.0f,  // 左下
             1.0f, -1.0f,  // 右下
            -1.0f, 1.0f,   // 左上
             1.0f, 1.0f    // 右上
        };
        static const GLfloat texCoords[] = {
            0.0f, 1.0f,    // 左下
            1.0f, 1.0f,    // 右下
            0.0f, 0.0f,    // 左上
            1.0f, 0.0f     // 右上
        };

        // 设置顶点属性
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices);
        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texCoords);
        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);

        // 绘制
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

        m_program.release();
    }

private:
    QOpenGLShaderProgram m_program;
    GLuint m_tex = 0;
    const uint8_t* m_data = nullptr;
    int m_width = 0;
    int m_height = 0;
    float m_aspect = 1.0f;
};


#endif // RGB32WIDGET_H
